package io.renren.modules.title.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.course.dao.ExamCourseMapper;
import io.renren.modules.course.entity.ExamCourse;
import io.renren.modules.title.dao.ExamOptionsMapper;
import io.renren.modules.title.dao.ExamTitleMapper;
import io.renren.modules.title.entity.ExamOptions;
import io.renren.modules.title.entity.ExamTitle;
import io.renren.modules.title.from.TitleFrom;
import io.renren.modules.title.service.ExamTitleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName ExamTitleImpl
 * @Author mengjiafeng
 * @Date 2019/9/4 10:37
 **/
@Service
public class ExamTitleImpl extends ServiceImpl<ExamTitleMapper, ExamTitle> implements ExamTitleService {
    @Autowired
    private ExamTitleMapper examTitleMapper;
    @Autowired
    private ExamOptionsMapper examOptionsMapper;
    @Autowired
    private ExamCourseMapper examCourseMapper;


    @Override
    public Map getTitleDetail(String id) {
        HashMap<Object, Object> titleMap = new HashMap<>();
        TitleFrom titleFrom = new TitleFrom();
        ExamTitle examTitle = examTitleMapper.selectById(id);
        BeanUtils.copyProperties(examTitle,titleFrom);
        if (examTitle.getType()==1){
            String[] s = titleFrom.getAnswer().split(", ");
            List<String> correctArray = Arrays.asList(s);
            titleFrom.setCorrectArray(correctArray);
        }
        List<ExamOptions> optionsList = examOptionsMapper.selectList(new QueryWrapper<ExamOptions>().eq("qid", id));
        titleMap.put("examTitle",titleFrom);
        titleMap.put("optionsList",optionsList);
        return titleMap;
    }

    @Override
    @Transactional
    public void updateTitle(TitleFrom titleFrom) {
        ExamTitle examTitle = new ExamTitle();
        BeanUtils.copyProperties(titleFrom,examTitle);
        examTitle.setUpdateTime(new Date());
        examTitleMapper.updateById(examTitle);
        for (Object o : titleFrom.getOptions()) {
            ExamOptions examOptions = new ExamOptions();
            BeanUtils.copyProperties(o,examOptions);
            examOptionsMapper.updateById(examOptions);
        }
    }

    @Override
    @Transactional
    public void addTitle(TitleFrom titleFrom) {
        if (titleFrom.getType()==1){
            String correct = titleFrom.getCorrectArray().toString().replace("[","").replace("]","");
            titleFrom.setAnswer(correct);
        }
        ExamCourse examCourse = examCourseMapper.selectByPrimaryKey(titleFrom.getCid());
        titleFrom.setCourse(examCourse.getName());
        ExamTitle examTitle = new ExamTitle();
        BeanUtils.copyProperties(titleFrom,examTitle);
        examTitle.setCreateTime(new Date());
        examTitle.setAuditState("0");
        examTitleMapper.insert(examTitle);
        for (Object o : titleFrom.getOptions()) {
            ExamOptions examOptions = new ExamOptions();
            BeanUtils.copyProperties(o,examOptions);
            examOptions.setQid(examTitle.getId());
            examOptionsMapper.insert(examOptions);
        }
    }

    @Override
    @Transactional
    public void deleteTitle(Long[] ids) {
        for (Long id : ids) {
            examOptionsMapper.delete(new QueryWrapper<ExamOptions>().eq("qid",id));
        }
        this.removeByIds(Arrays.asList(ids));
    }
}
